<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="amazonOrderId">
        <el-input
          v-model="queryParams.amazonOrderId"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市场编号" prop="marketplaceId">
        <el-select v-model="queryParams.marketplaceId" placeholder="请选择市场编号" clearable size="small">
          <el-option
            v-for="dict in marketplaceIdOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="购买时间" prop="purchaseDate">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.purchaseDate"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择购买时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-input
          v-model="queryParams.orderStatus"
          placeholder="请输入订单状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家名称" prop="addressCountyName">
        <el-input
          v-model="queryParams.addressCountyName"
          placeholder="请输入国家名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['amz:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amz:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['amz:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amz:order:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="订单主键自增" align="center" prop="orderId" />-->
      <el-table-column label="订单编号" align="center" prop="amazonOrderId" />
      <el-table-column label="市场编号" align="center" prop="marketplaceId" :formatter="marketplaceIdFormat" />
      <el-table-column label="购买时间" align="center" prop="purchaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.purchaseDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="订单总价" align="center" prop="moneyOrderTotal" />
      <el-table-column label="发货数量" align="center" prop="numberOfItemsShipped" />
      <el-table-column label="国家名称" align="center" prop="addressCountyName" />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" width="160" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
            v-hasPermi="['amz:order:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amz:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['amz:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号" prop="amazonOrderId">
          <el-input v-model="form.amazonOrderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="市场编号" prop="marketplaceId">
          <el-select v-model="form.marketplaceId" placeholder="请选择市场编号">
            <el-option
              v-for="dict in marketplaceIdOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购买时间" prop="purchaseDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.purchaseDate"
            type="date"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择购买时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单状态" prop="orderStatus">
          <el-input v-model="form.orderStatus" placeholder="请输入订单状态" />
        </el-form-item>
        <el-form-item label="交付渠道" prop="fulfillmentChannel">
          <el-input v-model="form.fulfillmentChannel" placeholder="请输入交付渠道" />
        </el-form-item>
        <el-form-item label="订单总价" prop="moneyOrderTotal">
          <el-input v-model="form.moneyOrderTotal" placeholder="请输入订单总价" />
        </el-form-item>
        <el-form-item label="货币代码" prop="moneyCurrencyCode">
          <el-input v-model="form.moneyCurrencyCode" placeholder="请输入货币代码" />
        </el-form-item>
        <el-form-item label="发货数量" prop="numberOfItemsShipped">
          <el-input v-model="form.numberOfItemsShipped" placeholder="请输入发货数量" />
        </el-form-item>
        <el-form-item label="买家名称" prop="buyerName">
          <el-input v-model="form.buyerName" placeholder="请输入买家名称" />
        </el-form-item>
        <el-form-item label="买家邮箱" prop="buyerEmail">
          <el-input v-model="form.buyerEmail" placeholder="请输入买家邮箱" />
        </el-form-item>
        <el-form-item label="买家电话" prop="buyerPhone">
          <el-input v-model="form.buyerPhone" placeholder="请输入买家电话" />
        </el-form-item>
        <el-form-item label="地址邮编" prop="addressPostalCode">
          <el-input v-model="form.addressPostalCode" placeholder="请输入地址邮编" />
        </el-form-item>
        <el-form-item label="地址1" prop="addressLine1">
          <el-input v-model="form.addressLine1" placeholder="请输入地址1" />
        </el-form-item>
        <el-form-item label="地址2" prop="addressLine2">
          <el-input v-model="form.addressLine2" placeholder="请输入地址2" />
        </el-form-item>
        <el-form-item label="地址3" prop="addressLine3">
          <el-input v-model="form.addressLine3" placeholder="请输入地址3" />
        </el-form-item>
        <el-form-item label="国家代码" prop="addressCountryCode">
          <el-input v-model="form.addressCountryCode" placeholder="请输入国家代码" />
        </el-form-item>
        <el-form-item label="国家名称" prop="addressCountyName">
          <el-input v-model="form.addressCountyName" placeholder="请输入国家名称" />
        </el-form-item>
        <el-form-item label="城市" prop="addressCity">
          <el-input v-model="form.addressCity" placeholder="请输入城市" />
        </el-form-item>
        <el-form-item label="地区" prop="addressStateOrRegion">
          <el-input v-model="form.addressStateOrRegion" placeholder="请输入地区" />
        </el-form-item>
        <el-form-item label="行政区" prop="addressDistrict">
          <el-input v-model="form.addressDistrict" placeholder="请输入行政区" />
        </el-form-item>
        <el-form-item label="直辖市" prop="addressMunicipality">
          <el-input v-model="form.addressMunicipality" placeholder="请输入直辖市" />
        </el-form-item>
        <el-form-item label="地址类型" prop="addressType">
          <el-input v-model="form.addressType" placeholder="请输入地址类型" />
        </el-form-item>
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/amz/order";

export default {
  name: "Order",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 市场编号字典
      marketplaceIdOptions: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        amazonOrderId: null,
        marketplaceId: null,
        purchaseDate: null,
        orderStatus: null,
        addressCountyName: null,
        deptName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        amazonOrderId: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        marketplaceId: [
          { required: true, message: "市场编号不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();

    this.getDicts("amz_marketplace_id").then(response => {
      this.marketplaceIdOptions = response.data;
    });

  },
  methods: {
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 市场编号字典翻译
    marketplaceIdFormat(row, column) {
      return this.selectDictLabel(this.marketplaceIdOptions, row.marketplaceId);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        amazonOrderId: null,
        marketplaceId: null,
        purchaseDate: null,
        orderStatus: null,
        fulfillmentChannel: null,
        moneyOrderTotal: null,
        moneyCurrencyCode: null,
        numberOfItemsShipped: null,
        buyerName: null,
        buyerEmail: null,
        buyerPhone: null,
        addressPostalCode: null,
        addressLine1: null,
        addressLine2: null,
        addressLine3: null,
        addressCountryCode: null,
        addressCountyName: null,
        addressCity: null,
        addressStateOrRegion: null,
        addressDistrict: null,
        addressMunicipality: null,
        addressType: null,
        deptId: null,
        deptName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail() {
      this.msgSuccess("暂时未开发，敬请等待！");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$confirm('是否确认删除订单信息编号为"' + orderIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(orderIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
